<template>
    <div class="vue-chosen form-group fg-line">
        <label class="m-b-6">{{ config.label }}</label>
        <select class="chosen" 
                :data-placeholder="config.placeholder"
                v-chosen="value"
                :disabled.sync="disabled"
        >
            <option v-for="option in opts" value={{option.value}}>{{option.display}}</option>
        </select>
    </div>
</template>

<script>
    const DEFAULT_OPTIONS = {
        "CURRENCY" : ['RMB','USD','EUR'],
        "PROJECT_LEVEL": [  
                            {value: 'A',display:Lang.get('project.allNew')},
                            {value: 'B',display:Lang.get('project.Innovation')},
                            {value: 'C',display:Lang.get('project.Cosmetics')}
                        ],
        "PROJECT_STATUS": [
                            { value:"4", display: "Pending customer approval"},
                            { value:"5", display: "Approved"},
                            { value:"6", display: "Rejected"},
                            { value:"7", display: "On Hold"},
                            { value:"8", display: "Closed"},
                            { value:"9", display: "Planning"},
                            { value:"10", display: "In progress"}
        ],
        "USER": {
            type: "ajax",
            api:  ""
        }

    };
    export default {
        props: ['config','value','disabled'],
        methods:{
            _prepareOptions () {
                let options = this.config.options;
                let result = [];
                // 指令 -> 指定options的定义
                if(_.isString(options)){
                    result = options = DEFAULT_OPTIONS[options];
                }
                // 数组
                if(_.isArray(options)){
                    result = options.map(function(item){
                        // 如果是基础类型
                        if(_.isNumber(item) || _.isString(item)){
                            return {
                                value:item,
                                display:item
                            }
                        }
                        //是否是Object并且包含value和display属性
                        if(_.isObject(item) 
                            && _.has(item,'display') 
                            && _.has(item,'value')){
                            return item;
                        }
                    });
                }
                return result;
            }
        },
        data () {
            var opts = this._prepareOptions();
            return {
                opts,
                isAjax: opts.type == "ajax",
                ajaxAPI: this.config.api
            }
        }
        
    }
</script>
<style lang="sass">
    .m-b-6{
        margin-bottom:6px;
    }

</style>